package com.sslpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sslpay.common.domain.BusinessException;
import com.sslpay.common.domain.CommonErrorCode;
import com.sslpay.common.util.PhoneUtil;
import com.sslpay.merchant.api.MerchantService;
import com.sslpay.merchant.api.dto.MerchantDTO;
import com.sslpay.merchant.convert.MerchantConvert;
import com.sslpay.merchant.entity.Merchant;
import com.sslpay.merchant.mapper.MerchantMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 描述：商户service
 *
 * @author ssl
 * @create 2020/04/15 14:02
 */
@org.apache.dubbo.config.annotation.Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 根据id查找商户
     *
     * @param id
     * @return
     */
    @Override
    public MerchantDTO getMerchantById(Long id) throws BusinessException {
        if (null == id) {
            throw new BusinessException(CommonErrorCode.E_200019);
        }
        Merchant merchant = merchantMapper.selectById(id);
        if (null == merchant) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        return MerchantConvert.INSTANCE.entityToDTO(merchant);
    }

    /**
     * 商户注册
     *
     * @param merchantDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MerchantDTO createMerchant(MerchantDTO merchantDTO) throws BusinessException {
        if (merchantDTO == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        if (StringUtils.isBlank(merchantDTO.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        if (!PhoneUtil.isMatches(merchantDTO.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }
        //联系人非空校验
        if (StringUtils.isBlank(merchantDTO.getUsername())) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        //密码非空校验
        if (StringUtils.isBlank(merchantDTO.getPassword())) {
            throw new BusinessException(CommonErrorCode.E_100111);
        }
        //校验手机号的唯一性
        //根据手机号查询商户表，如果存在记录则说明手机号已存在
        Integer count = merchantMapper.selectCount(new LambdaQueryWrapper<Merchant>()
                .eq(Merchant::getMobile, merchantDTO.getMobile()));
        if (0 < count) {
            throw new BusinessException(CommonErrorCode.E_100113);
        }
        // dto - entity
        Merchant merchant = MerchantConvert.INSTANCE.dtoToEntity(merchantDTO);
        // 设置审核状态0‐未申请,1‐已申请待审核,2‐审核通过,3‐审核拒绝
        merchant.setAuditStatus("0");
        merchantMapper.insert(merchant);
        return MerchantConvert.INSTANCE.entityToDTO(merchant);
    }

    /**
     * 资质申请
     *
     * @param merchantId  商户ID
     * @param merchantDTO 资质申请信息
     */
    @Override
    public void applyMerchant(Long merchantId, MerchantDTO merchantDTO) throws BusinessException {
        if (null == merchantId || null == merchantDTO) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (null == merchant) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        // dto - entity
        Merchant entity = MerchantConvert.INSTANCE.dtoToEntity(merchantDTO);
        entity.setId(merchantId);
        entity.setMobile(merchantDTO.getMobile());
        //审核状态1-已申请待审核
        entity.setAuditStatus("1");
        //租户id
        entity.setTenantId(merchantDTO.getTenantId());
        merchantMapper.updateById(entity);
    }
}
